package com.cheyun.netsalev3.view

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.DataX
import com.cheyun.netsalev3.databinding.ActivityCallRecordBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.CallRecordActivityViewModel

class CallRecordActivity : BaseActivity<ActivityCallRecordBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_call_record
    }

    override fun initViewModel(): ViewModel? {
        var phone = intent.getStringExtra("phone") ?: ""
        var factory = InjectorUtils.getCallRecordActivityViewModelFactory(phone)
        return ViewModelProviders.of(this, factory).get(CallRecordActivityViewModel::class.java)
    }

    private lateinit var adapter: MyPagedListAdapter<DataX>

    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.call_record_activity_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }

    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter.setNetworkState(it)
        })
    }

}
